package com.jeesite.modules.dataScreen.config;

import com.github.benmanes.caffeine.cache.Caffeine;
import com.jeesite.modules.dataScreen.enums.CacheEnum;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.ArrayList;
import java.util.List;

@EnableScheduling
@EnableCaching
@Configuration
public class CacheConfig {


    @Bean
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        List<CaffeineCache> caches = new ArrayList<>();
        for (CacheEnum value : CacheEnum.values()) {
            CaffeineCache cache = new CaffeineCache(value.getName(),
                    Caffeine.newBuilder().initialCapacity(50)
                            .maximumSize(1000)
                            .expireAfterAccess(value.getDuration())
                            .build());
            caches.add(cache);
        }
        cacheManager.setCaches(caches);
        return cacheManager;
    }
}
